import axios from "axios";
const request = axios.create({
  baseURL: "http://www.tangxiaoyang.vip:8888/api/v2",
  timeout: 1000 * 10,
});
//请求拦截器
request.interceptors.request.use(
  (config) => {
    config.headers.Authorization = localStorage.getItem("token") || "";
    return config;
  },
  (error) => Promise.reject(error)
);
//响应拦截器
request.interceptors.response.use(
  (config) => {
    const { data, meta } = config.data;
    if (meta.status == "200" || "201" || "204") {
      if (data?.token) {
        localStorage.setItem("token", data.token);
      }
      return config.data;
    } else {
      ElMessage({
        message: meta.msg,
        type: "error",
      });
    }
  },
  (error) => Promise.reject(error)
);
export default request;
